package com.dhcc.bpm.modules.cost.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.common.enums.ConfigType;
import com.dhcc.bpm.common.redisMessage.RedisMessageSender;
import com.dhcc.bpm.modules.api.entity.Product;
import com.dhcc.bpm.modules.api.service.IApiService;
import com.dhcc.bpm.modules.api.service.IProductService;
import com.dhcc.bpm.modules.cost.entity.CostRule;
import com.dhcc.bpm.modules.cost.service.ICostRuleService;
import com.dhcc.bpm.modules.cost.vo.CostRuleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * @ClassName CostRuleController.class
 * @Description  收费规则表 控制器
 * @Author xiaFaYu
 * @Date 2019-12-16 14:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-cost/costRule")
@Api(value = "收费规则表", tags = "收费规则表接口")
public class CostRuleController extends BladeController {
	private static final Logger log = LoggerFactory.getLogger(CostRuleController.class);

	private ICostRuleService costRuleService;

	private IProductService productService;

	private IApiService apiService;
	@Autowired
	private RedisMessageSender redisMessageSender;

	/**
	 * @Description 详情
	 * @Author xiafayu
	 * @Date 2020/2/24 15:01
	 * @param costRule
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.cost.entity.CostRule>
	 **/
	@ApiLog("收费规则详情")
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入costRule")
	@Log(title = "查看详情", businessType = BusinessType.SELECT)
	public R<CostRule> detail(CostRule costRule) {
		CostRule detail = costRuleService.getOne(Condition.getQueryWrapper(costRule));
		return R.data(detail);
	}

	/**
	 * @Description 分页 收费规则表
	 * @Author xiafayu
	 * @Date 2020/2/24 15:01
	 * @param costRule
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.cost.entity.CostRule>
	 **/
	@ApiLog("收费规则分页")
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入costRule")
	public R<IPage<CostRule>> list(CostRule costRule, Query query) {
		IPage<CostRule> pages = costRuleService.page(Condition.getPage(query), Condition.getQueryWrapper(costRule));
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 收费规则表
	 * @Author xiafayu
	 * @Date 2020/2/24 15:01
	 * @param costRule
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.cost.entity.CostRule>
	 **/
	@ApiLog("收费规则分页")
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入costRule")
	public R<IPage<CostRuleVO>> page(CostRuleVO costRule, Query query) {
		IPage<CostRuleVO> pages = costRuleService.selectCostRulePage(Condition.getPage(query), costRule);
		return R.data(pages);
	}

	/**
	 * @Description 新增 收费规则表
	 * @Author xiafayu
	 * @Date 2020/2/24 15:01
	 * @param costRule
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.cost.entity.CostRule>
	 **/
	@ApiLog("新增收费规则")
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入costRule")
	@Log(title = "新增收费规则", businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody CostRule costRule) {
		costRule.setRefreshFlag(0);
		return R.status(costRuleService.save(costRule));
	}

	/**
	 * @Description 修改 收费规则表
	 * @Author xiafayu
	 * @Date 2020/2/24 15:01
	 * @param costRule
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.cost.entity.CostRule>
	 **/
	@ApiLog("修改收费规则")
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入costRule")
	@Log(title = "修改收费规则", businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody CostRule costRule) {
		costRule.setRefreshFlag(0);
		return R.status(costRuleService.updateById(costRule));
	}

	/**
	 * @Description 新增或修改 收费规则表
	 * @Author xiafayu
	 * @Date 2020/2/24 15:01
	 * @param costRule
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.cost.entity.CostRule>
	 **/
	@ApiLog("新增修改收费规则")
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入costRule")
	@Log(title = "新增修改收费规则", businessType = BusinessType.UPDATE)
	public R submit(@Valid @RequestBody CostRule costRule) {
		costRule.setRefreshFlag(0);
		return R.status(costRuleService.saveOrUpdate(costRule));
	}
	/**
	 * @Description 删除 收费规则表
	 * @Author xiafayu
	 * @Date 2020/2/24 15:02
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("删除收费规则")
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@Log(title = "删除收费规则", businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(costRuleService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	* @author: 陈泽
	* @date: 2019/12/16 17:25
	* @description: 通过产品编号获取收费规则
	* @param: [productId] 
	* @return: org.springblade.core.tool.api.R<com.dhcc.bpm.modules.cost.vo.CostRuleVO>
	*/
	@ApiLog("查询收费规则")
	@GetMapping("/getDetailByProductId")
	@Log(title = "查询收费规则", businessType = BusinessType.SELECT)
	public R<CostRuleVO> getDetailByProductId(Long id){
		Product product = productService.getById(id);
		CostRuleVO detail = costRuleService.getByProductId(id);
		if(detail == null){
			detail = new CostRuleVO();
		}
		detail.setProductName(product.getProductName());
		detail.setProductCode(product.getProductCode());
		detail.setIsCost(product.getIsCost());
		return R.data(detail);
	}

	/**
	* @author: 陈泽
	* @date: 2019/12/16 17:25
	* @description: 
	* @param: [ApitId] 
	* @return: org.springblade.core.tool.api.R<com.dhcc.bpm.modules.cost.vo.CostRuleVO>
	*/
	@ApiLog("查询收费规则")
	@GetMapping("/getDetailByApiId")
	@Log(title = "查询收费规则", businessType = BusinessType.SELECT)
	public R<CostRuleVO> getDetailByApiId(Long id){
		com.dhcc.bpm.modules.api.entity.Api api = apiService.getById(id);
		CostRuleVO detail = costRuleService.getByApiId(id);
		if(detail == null){
			detail = new CostRuleVO();
		}
		detail.setApiName(api.getApiName());
		detail.setApiCode(api.getApiCode());
		detail.setIsCost(api.getIsCost());
		return R.data(detail);
	}

	/**
	 * @Description 刷新扣费配置
	 * @Author xiafayu
	 * @Date 2020/2/24 15:03
	 * @param opcProductId
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("刷新费用配置")
	@PostMapping("/refreshCostRule")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "刷新费用配置", notes = "费用配置")
	@Log(title = "刷新费用配置", businessType = BusinessType.REFRESH)
	public R refreshCostRule(Long opcProductId) {
		String message = redisMessageSender.format(ConfigType.API.name(), "刷新费用配置信息");
		redisMessageSender.sendMessageToGateway("", message);
		CostRuleVO costRuleVO = new CostRuleVO();
		costRuleVO.setOpcProductId(opcProductId);
		costRuleVO.setRefreshFlag(1);
		costRuleService.updateRefreshStatus();
		return R.status(true);
	}
}
